package com.fh.shop.api.mq.consumer;

import com.fh.shop.api.mq.config.MQConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class HelloConsumer {

    @RabbitListener(queues = MQConstant.TestDirect.QUEUE)
    public void handleDirectMsg(String msg, Message message, Channel channel) throws IOException {
        // 获取消息标记
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 业务逻辑
        try {
            log.info("接受{}消息:{}", MQConstant.TestDirect.QUEUE, msg);
            if (msg.contains("1")) {
                throw new RuntimeException(msg+"错误!!!");
            }
        } catch (RuntimeException e) {
            // 打印错误日志
            log.error("消息：{},出错了，原因为:{}", msg, e.getMessage());
            // 记录到数据库表中[人工处理][sql语句]
            // id 时间 消息内容 错误原因
        } finally {
            // 手动ack
            channel.basicAck(deliveryTag, false);
        }
//        if (msg.contains("1")) {
//            throw new RuntimeException(msg+"错误!!!");
//        }
    }

//    @RabbitListener(queues = MQConstant.TestDirect.BLUE_QUEUE)
//    public void handleBlueMsg(String msg) {
//        log.info("接受{}消息:{}", MQConstant.TestDirect.BLUE_QUEUE, msg);
//    }

//    @RabbitListener(queues = MQConstant.TestDirect.QUEUE1)
//    public void handleQueue1Msg(String msg) {
//        log.info("接受{}消息:{}", MQConstant.TestDirect.QUEUE1, msg);
//    }
//
//    @RabbitListener(queues = MQConstant.TestFanout.QUEUE1)
//    public void handleQ1Msg(String msg) {
//        log.info("接受{}消息:{}",  MQConstant.TestFanout.QUEUE1, msg);
//    }
//
//    @RabbitListener(queues = MQConstant.TestFanout.QUEUE2)
//    public void handleQ2Msg(String msg) {
//        log.info("接受{}消息:{}",  MQConstant.TestFanout.QUEUE2, msg);
//    }
//
//    @RabbitListener(queues = MQConstant.TestTopic.Q4)
//    public void handleQ4Msg(String msg) {
//        log.info("接受{}消息:{}",  MQConstant.TestTopic.Q4, msg);
//    }
//
//    @RabbitListener(queues = MQConstant.TestTopic.Q5)
//    public void handleQ5Msg(String msg) {
//        log.info("接受{}消息:{}",  MQConstant.TestTopic.Q5, msg);
//    }

}
